package com.mdh.runerrands.serviceImpl;

import com.mdh.runerrands.dao.AddressDao;
import com.mdh.runerrands.dao.BaseUserDao;
import com.mdh.runerrands.pojo.Address;
import com.mdh.runerrands.pojo.AddressWithCustomer;
import com.mdh.runerrands.pojo.BaseUser;
import com.mdh.runerrands.service.AddressService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    @Resource
    private AddressDao addressDao;
    @Resource
    private BaseUserDao baseUserDao;
    @Override
    public Address findById(int id) {
        return addressDao.selectById(id);
    }

    @Override
    public boolean deleteById(int id) {
        return addressDao.deleteById(id);
    }

    @Override
    public List<Address> findByCustomerId(int customerId) {
        return addressDao.selectByCustomerId(customerId);
    }

    @Override
    public List findAllAddressWithCustomer(){
        List<Address> address = addressDao.selectAllAddress();

        List list = new ArrayList();
        for (Address s :address){
            if (s == null){
                break;
            }
            AddressWithCustomer addressWithCustomer = new AddressWithCustomer();
            BaseUser user = baseUserDao.selectById(s.getCustomerId());
            addressWithCustomer.setId(s.getId());
            addressWithCustomer.setProvince(s.getProvince());
            addressWithCustomer.setTelephone(s.getTelephone());
            addressWithCustomer.setAddress(s.getAddress());
            addressWithCustomer.setArea(s.getArea());
            addressWithCustomer.setCity(s.getCity());
            addressWithCustomer.setCustomerId(s.getCustomerId());
            addressWithCustomer.setBaseUser(user);
            list.add(addressWithCustomer);
        }
        return  list ;
    }

    @Override
    public boolean insert(Address address) {
        return addressDao.insert(address);
    }

    @Override
    public boolean update(Address address) {
        return addressDao.update(address);
    }

    @Override
    public boolean batchDelete(int ...ids) {
        return addressDao.batchDelete(ids);
    }

    @Override
    public List<Address> queryPage(int page, int pageSize, String province, String city, String area) {
        page = (page-1)*pageSize;
        return addressDao.queryPage(page,pageSize,province,city,area);
    }

    @Override
    public int selectCount(String province, String city, String area) {
        return addressDao.selectCount(province,city,area);
    }

}
